import { useRef } from "react";
import { message, Modal } from "antd";

import InfoForm from "./InfoForm";
import { useAppSelector } from "@/utils/hooks";

import type { RecordItem } from "../type";
import { roleTypeMap } from "@/utils/consts";
import request from "@/utils/request";

interface Props {
	open?: boolean;
	selRecord?: RecordItem;
	onCancel: () => void;
	onSearch?: (params: any) => void;
}

const InfoEditModal: React.FC<Props> = ({ open, onCancel, selRecord, onSearch }) => {
	const [app] = useAppSelector(state => [state.app]);
	const infoFromRef = useRef<any>(null);

	const handleCancel = () => {
		onCancel();
	};

	const handleOk = async () => {
		try {
			const values = await infoFromRef.current.validateFields();
			request("tenantManageEdit", {
				data: {
					...values,
					tenantId: selRecord?.tenantId
				}
			}).then(res => {
				if (res.code === 0) {
					message.success("修改成功");
					onSearch?.({});
					onCancel();
				}
			});
		} catch (error) {
			console.log(error);
		}
	};

	return (
		<Modal
			title="租户信息修改"
			width={640}
			maskClosable={false}
			open={open}
			bodyStyle={{ maxHeight: 550, overflowY: "auto" }}
			onCancel={handleCancel}
			onOk={handleOk}
		>
			<InfoForm ref={infoFromRef} selRecord={selRecord}></InfoForm>
		</Modal>
	);
};

export default InfoEditModal;
